<?php
/**
 * Created by PhpStorm.
 * User: yaolei
 * Date: 2017/11/16
 * Time: 23:35
 */

namespace Home\Model;
use Think\Model;

class Order extends Model
{
    protected $tableName = 'balance_order';
    public function getData(){
        $User = M("user")->find($this->user_id);
        $data=$this->data;
        $data['user']=$User;
        $goods = M("balance_goods")->find($this->balance_goods_id);
        $data['goods']=$goods;
        return $data;
    }

    public function update(){
        $model = new Model();
        $order=$model->table(C('DB_PREFIX').'balance_order')->where("id=".$this->id)->find();
        if($order['order_status']!=0){
            $this->error="只有待支付订单才能取消";
            return false;
        }
        $model->startTrans();
        $flag=false;
        $res=$model->table(C('DB_PREFIX').'balance_order_log')->add([
            'order_id'=>$this->id,
            'description'=>"管理员，已经取消订单",
            'createdtime'=>time()
        ]);
        if($res){
            $flag=true;
        }
        if(!$flag){
            $model->rollback();
        }else{
            $model->table(C('DB_PREFIX').'balance_order')->where("id=".$this->id)->save([
                'updatedtime'=>time(),
                'canceltime'=>time(),
                'order_status'=>$this->order_status
            ]);
            $model->commit();
        }

        return $flag;
    }
}